package cn.xabad.commons.sample.http;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;

import cn.xabad.commons.converter.StringConverterFactory;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;

public class RestAdapter {

    private static Retrofit stringInstance;
    private static Retrofit gsonInstance;
    private static Retrofit rxStringInstance;
    private static Retrofit rxGsonInstance;
    private static OkHttpClient client;


    public static Retrofit getRxStringInstance() {
        if (client == null) {
            client = new OkHttpClient();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
            client.interceptors()
                  .add(interceptor);
        }

        if (rxStringInstance == null) {
            rxStringInstance = new Retrofit.Builder().baseUrl("http://api.boxfish.cn")
                                                     .client(client)
                                                     .addConverterFactory(new StringConverterFactory())
                                                     .addCallAdapterFactory
                                                             (RxJavaCallAdapterFactory.create())
                                                     .build();
        }
        return rxStringInstance;
    }
    public static Retrofit getRxGsonInstance() {
        if (client == null) {
            client = new OkHttpClient();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
            client.interceptors()
                  .add(interceptor);
        }

        if (rxGsonInstance == null) {
            rxGsonInstance = new Retrofit.Builder().baseUrl("http://api.boxfish.cn")
                                                     .client(client)
                                                     .addConverterFactory(GsonConverterFactory.create())
                                                     .addCallAdapterFactory
                                                             (RxJavaCallAdapterFactory.create())
                                                     .build();
        }
        return rxGsonInstance;
    }


    public static Retrofit getStringInstance() {
        if (client == null) {
            client = new OkHttpClient();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            client.interceptors()
                  .add(interceptor);
        }

        if (stringInstance == null) {
            stringInstance = new Retrofit.Builder().baseUrl("http://api.boxfish.cn")
                                                   .client(client)
                                                   .addConverterFactory(new StringConverterFactory())
                                                   .build();
        }
        return stringInstance;
    }


    public static Retrofit getGsonInstance() {
        if (client == null) {
            client = new OkHttpClient();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            client.interceptors()
                  .add(interceptor);
        }

        if (gsonInstance == null) {
            gsonInstance = new Retrofit.Builder().baseUrl("http://api.boxfish.cn")
                                                 .client(client)
                                                 .addConverterFactory(GsonConverterFactory.create())
                                                 .build();
        }
        return gsonInstance;
    }


    public static void clean() {
        stringInstance = null;
        gsonInstance = null;
        rxStringInstance = null;
        rxGsonInstance = null;
        client = null;
    }
}